package com.itxiuyixiu.homework.admin.controller;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.itxiuyixiu.homework.admin.service.AdminMessageService;
import com.itxiuyixiu.homework.entity.dto.AcademyDto;
import com.itxiuyixiu.homework.entity.dto.MajorDto;
import com.itxiuyixiu.homework.entity.dto.StudentDto;
import com.itxiuyixiu.homework.entity.dto.TeacherDto;
import com.itxiuyixiu.homework.entity.model.Student;
import com.itxiuyixiu.homework.entity.vo.AcademyVo;
import com.itxiuyixiu.homework.entity.vo.MajorVo;
import com.itxiuyixiu.homework.entity.vo.StudentVo;
import com.itxiuyixiu.homework.entity.vo.TeacherVo;
import com.itxiuyixiu.homework.utils.ReturnUtils.ReturnMessage;
import com.itxiuyixiu.homework.utils.ReturnUtils.ReturnVo;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.websocket.server.PathParam;
import java.io.*;
import java.util.List;

/**
 * @author 黄磊
 * @date 2020/3/9
 */
@Api(tags = "管理员-信息管理系统")
@RestController
public class AdminMessageController {
    private final AdminMessageService adminMessageService;

    public AdminMessageController(AdminMessageService adminMessageService) {
        this.adminMessageService = adminMessageService;
    }

    @ApiOperation(value = "获取学生所有信息" , response = Student.class)
    @GetMapping("/admin/message/student/{studentId}")
    public ReturnVo getStudentMessage(@PathVariable String studentId) {
        Object student = adminMessageService.getStudentMessage(studentId);
        if (student == null) {
            return ReturnMessage.getFailureReturn("学号不存在");
        }
        return ReturnMessage.getSuccessReturn(student);
    }

    @ApiOperation(value = "获取学生信息" , response = StudentVo.class , responseContainer = "List", notes = "使用studentId、studentName、identityCard、academyId来进行查询。其他字段无效")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "studentId" , value = "学生id"),
            @ApiImplicitParam(name = "studentName" , value = "学生名字"),
            @ApiImplicitParam(name = "identityCard" , value = "身份证号" ,dataType = "int"),
            @ApiImplicitParam(name = "academyId" , value = "学院id" , dataType = "int")
    })
    @GetMapping("/admin/message/student")
    public ReturnVo getStudentMessage(StudentDto dto) {
        return ReturnMessage.getSuccessReturn(adminMessageService.getStudentMessage(dto));
    }

    @Autowired
    private ObjectMapper objectMapper;

    @ApiOperation(value = "获取老师信息" , response = TeacherVo.class , responseContainer = "List" ,notes = "使用id、teacherName来进行查询。其他字段无效")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id" , value = "老师id" , dataType = "int"),
            @ApiImplicitParam(name = "teacherName" , value = "老师名字" ,readOnly = true),
    })
    @GetMapping("/admin/message/teacher")
    public ReturnVo getTeacherMessage(TeacherDto dto) throws Exception{
        List<TeacherVo> teacherMessage = adminMessageService.getTeacherMessage(dto);
//        System.out.println(objectMapper.writeValueAsString(teacherMessage.get(0)));
        return ReturnMessage.getSuccessReturn(teacherMessage);
    }

    @ApiOperation(value = "获取学院信息" , response = AcademyVo.class , responseContainer = "List" , notes = "使用id、academyName来进行查询")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id" , value = "学院id" , dataType = "int"),
            @ApiImplicitParam(name = "academyName" , value = "学院名字"),
    })
    @GetMapping("/admin/message/academy")
    public ReturnVo getAcademyMessage(AcademyDto dto) {
        return ReturnMessage.getSuccessReturn(adminMessageService.getAcademyMessage(dto));
    }

    @ApiOperation(value = "获取专业信息" , response = MajorVo.class , responseContainer = "List" , notes = "使用id、majorName、academyId来进行查询")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id" , value = "专业id" , dataType = "int"),
            @ApiImplicitParam(name = "majorName" , value = "专业名字"),
            @ApiImplicitParam(name = "academyId" , value = "学院id")
    })
    @GetMapping("/admin/message/major")
    public ReturnVo getMajorMessage(@Validated MajorDto dto) {
        return ReturnMessage.getSuccessReturn(adminMessageService.getMajorMessage(dto));
    }

    @ApiOperation(value = "添加学生信息" , response = String.class)
    @PostMapping("/admin/message/student")
    public ReturnVo addStudentMessage(StudentDto dto) {
        switch (adminMessageService.addStudentMessage(dto)) {
            case 0:
                return ReturnMessage.getSuccessReturn("成功添加学生信息");
            case 1:
                return ReturnMessage.getFailureReturn("学院不存");
            case 2:
                return ReturnMessage.getFailureReturn("专业不存在");
            default:
                return ReturnMessage.getFailureReturn("发生未知错误");
        }
    }

    @ApiOperation(value = "添加老师信息" , response = String.class)
    @PostMapping("/admin/message/teacher")
    public ReturnVo addTeacherMessage(TeacherDto dto) {
        switch (adminMessageService.addTeacherMessage(dto)) {
            case 0:
                return ReturnMessage.getSuccessReturn("成功添加老师信息");
            case 1:
                return ReturnMessage.getFailureReturn("学院不存");
            default:
                return ReturnMessage.getFailureReturn("发生未知错误");
        }
    }

    @ApiOperation(value = "添加学院信息" , response = String.class)
    @PostMapping("/admin/message/academy")
    public ReturnVo addAcademyMessage(AcademyDto dto) {
        switch (adminMessageService.addAcademyMessage(dto)) {
            case 0:
                return ReturnMessage.getSuccessReturn("成功添加学院信息");
            case 1:
                return ReturnMessage.getFailureReturn("学院已经存在");
            default:
                return ReturnMessage.getFailureReturn("发生未知错误");
        }
    }

    @ApiOperation(value = "添加专业信息" , response = String.class)
    @PostMapping("/admin/message/major")
    public ReturnVo addMajorMessage(MajorDto dto) {
        switch (adminMessageService.addMajorMessage(dto)) {
            case 0:
                return ReturnMessage.getSuccessReturn("成功添加专业信息");
            case 1:
                return ReturnMessage.getFailureReturn("学院不存在");
            case 2:
                return ReturnMessage.getFailureReturn("专业已经存在");
            default:
                return ReturnMessage.getFailureReturn("发生未知错误");
        }
    }

    @ApiOperation(value = "修改学生信息" , response = String.class)
    @PutMapping("/admin/message/student/{studentId}")
    public ReturnVo modifyStudentMessage(@PathVariable("studentId") String studentId, StudentDto dto) {
        switch (adminMessageService.modifyStudentMessage(studentId,dto)) {
            case 0:
                return ReturnMessage.getSuccessReturn("成功修改学生信息");
            case 1:
                return ReturnMessage.getFailureReturn("学生不存在");
            case 2:
                return ReturnMessage.getFailureReturn("学院不存在");
            case 3:
                return ReturnMessage.getFailureReturn("专业不存在");
            default:
                return ReturnMessage.getFailureReturn("发生未知错误");
        }
    }

    @ApiOperation(value = "修改老师信息" , response = String.class)
    @PutMapping("/admin/message/teacher/{teacherId}")
    public ReturnVo modifyTeacherMessage(@PathVariable("teacherId") int teacherId, TeacherDto dto) {
        switch (adminMessageService.modifyTeacherMessage(teacherId,dto)) {
            case 0:
                return ReturnMessage.getSuccessReturn("成功修改教师信息");
            case 1:
                return ReturnMessage.getFailureReturn("教师不存在");
            case 2:
                return ReturnMessage.getFailureReturn("学院不存在");
            default:
                return ReturnMessage.getFailureReturn("发生未知错误");
        }
    }

    @ApiOperation(value = "修改学院信息" , response = String.class)
    @PutMapping("/admin/message/academy/{academyId}")
    public ReturnVo modifyAcademyMessage(@PathVariable("academyId") int academyId, AcademyDto dto) {
        switch (adminMessageService.modifyAcademyMessage(academyId,dto)) {
            case 0:
                return ReturnMessage.getSuccessReturn("成功修改学院信息");
            case 1:
                return ReturnMessage.getFailureReturn("学院不存在");
            default:
                return ReturnMessage.getFailureReturn("发生未知错误");
        }
    }

    @ApiOperation(value = "修改专业信息" , response = String.class)
    @PutMapping("/admin/message/major/{majorId}")
    public ReturnVo modifyMajorMessage(@PathVariable("majorId") int majorId, MajorDto dto) {
        switch (adminMessageService.modifyMajorMessage(majorId,dto)) {
            case 0:
                return ReturnMessage.getSuccessReturn("成功修改学院信息");
            case 1:
                return ReturnMessage.getFailureReturn("专业不存在");
            case 2:
                return ReturnMessage.getFailureReturn("学院不存在");
            default:
                return ReturnMessage.getFailureReturn("发生未知错误");
        }
    }

    @ApiOperation(value = "删除学生" , response = String.class)
    @DeleteMapping("/admin/message/student/{studentId}")
    public ReturnVo deleteStudent(@PathVariable("studentId") String studentId) {
        switch (adminMessageService.deleteStudent(studentId)) {
            case 0:
                return ReturnMessage.getSuccessReturn("成功删除学生信息");
            case 1:
                return ReturnMessage.getFailureReturn("学生不存在，请输入正确的学号");
            case 2:
                return ReturnMessage.getFailureReturn("存在奖励记录，请先删除其奖励记录");
            case 3:
                return ReturnMessage.getFailureReturn("存在课程情况，请先删除其课程情况");
            default:
                return ReturnMessage.getFailureReturn("发生未知错误");
        }
    }

    @ApiOperation(value = "删除老师" , response = String.class)
    @DeleteMapping("/admin/message/teacher/{teacherId}")
    public ReturnVo deleteTeacher(@PathVariable("teacherId") int teacherId) {
        switch (adminMessageService.deleteTeacher(teacherId)) {
            case 0:
                return ReturnMessage.getSuccessReturn("成功删除教师信息");
            case 1:
                return ReturnMessage.getFailureReturn("教师不存在");
            case 2:
                return ReturnMessage.getFailureReturn("存在课程记录，请先删除其课程记录");
            default:
                return ReturnMessage.getFailureReturn("发生未知错误");
        }
    }

    @ApiOperation(value = "删除学院" , response = String.class)
    @DeleteMapping("/admin/message/academy/{academyId}")
    public ReturnVo deleteAcademy(@PathVariable("academyId") int academyId) {
        switch (adminMessageService.deleteAcademy(academyId)) {
            case 0:
                return ReturnMessage.getSuccessReturn("成功删除学院信息");
            case 1:
                return ReturnMessage.getFailureReturn("学院不存在");
            case 2:
                return ReturnMessage.getFailureReturn("存在学生，请先删除所属学生");
            case 3:
                return ReturnMessage.getFailureReturn("存在专业，请先删除所属专业");
            case 4:
                return ReturnMessage.getFailureReturn("存在老师，请先删除所属老师");
            default:
                return ReturnMessage.getFailureReturn("发生未知错误");
        }
    }

    @ApiOperation(value = "删除专业" , response = String.class)
    @DeleteMapping("/admin/message/major/{majorId}")
    public ReturnVo deleteMajor(@PathVariable("majorId") int majorId) {
        switch (adminMessageService.deleteMajor(majorId)) {
            case 0:
                return ReturnMessage.getSuccessReturn("成功修改学院信息");
            case 1:
                return ReturnMessage.getFailureReturn("专业不存在");
            case 2:
                return ReturnMessage.getFailureReturn("存在学生，请先删除所属学生");
            case 3:
                return ReturnMessage.getFailureReturn("存在课程，请先删除所属课程");
            default:
                return ReturnMessage.getFailureReturn("发生未知错误");
        }
    }
}
